package org.orion.engine.demo;

/**
 *  Copyright 2007, by Prashant Bhat(prashant.mr@gmail.com).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.SwingUtilities;

/**
 * Utility class to switch between full-screen and normal mode for any jframe.
 * @author Prashant Bhat
 * @since 0.1 - Dec 22, 2007 6:38:02 PM
 * @version 0.1
 */
public class ScreenMode {
    private final JFrame jframe;
    private final DisplayMode displayMode;
    
    private Rectangle restoreBounds;
    private boolean isUndecorated = true;
    private boolean isFullScreenMode = false;
    private boolean isSwitchingFullScreenMode = false;
    private int restoreExtendedState = JFrame.NORMAL;
    private int windowDecorationStyle = JRootPane.FRAME;

    /**
     * Creates a full-screen switcher with the given frame.
     * @param jframe the frame to be displayed
     */
    public ScreenMode(JFrame jframe, DisplayMode displayMode) {
        this.jframe = jframe;
        this.displayMode = displayMode;
    }

    /**
     * Check if full screen mode is set.
     * @return <code>true</code> if in frame screen mode, else <code>false</code>
     */
    public boolean isFullScreenMode() {
        return isFullScreenMode;
    }

    /**
     * Switch between full screen mode or normal state.
     */
    public void setFullScreenMode() {
        setFullScreenMode(!isFullScreenMode);
    }

    /**
     * Set to full screen mode or normal state.
     * @param fullScreenMode <code>true</code> to switch to full screen mode
     */
    private void setFullScreenMode(boolean fullScreenMode) {
        if (isFullScreenMode == fullScreenMode || isSwitchingFullScreenMode) {
            return;
        }
        isSwitchingFullScreenMode = true;
        if (!isFullScreenMode) {
            restoreExtendedState = jframe.getExtendedState();
            restoreBounds = jframe.getBounds();
            isUndecorated = jframe.isUndecorated();
            windowDecorationStyle = jframe.getRootPane().getWindowDecorationStyle();
        }
        isFullScreenMode = fullScreenMode;
        jframe.setVisible(false);
        jframe.dispose();

        jframe.setUndecorated(isFullScreenMode || isUndecorated);
        // Added to support Custom Look and Feel with Decorations
        jframe.getRootPane().setWindowDecorationStyle(isFullScreenMode ? JRootPane.NONE : windowDecorationStyle);

        final boolean updateBounds = (!isFullScreenMode); // && restoreExtendedState != JFrame.MAXIMIZED_BOTH );
        
        
        GraphicsDevice graphicsDevice = jframe.getGraphicsConfiguration().getDevice();
        
        if (null != graphicsDevice && graphicsDevice.isFullScreenSupported()) {
            try {
        		graphicsDevice.setFullScreenWindow(isFullScreenMode ? jframe : null);
        		if ( displayMode!=null ) graphicsDevice.setDisplayMode(displayMode);
            } finally {
				System.out.println("Error");
			}
				
				
        }
        jframe.setExtendedState(isFullScreenMode ? JFrame.MAXIMIZED_BOTH : restoreExtendedState);
        jframe.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                jframe.invalidate();
                jframe.validate();
                jframe.repaint();
                if (updateBounds) {
                    jframe.setPreferredSize(restoreBounds.getSize());
                    jframe.setBounds(restoreBounds);
                }
                isSwitchingFullScreenMode = false;
            }
        });
    }
}




 

